;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Purchase.NCMMaintain", [])
        .controller("NCMMaintainCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "$filter", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, $filter, UserContextService, $stateParams) {
            $scope.activeFlag = [true, false, false, false, false];
            $scope.datePickerFlag = [false, false];
            $scope.markInfo = {
                allCheckedBill: false,
                allCheckedMaterial: false,
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.CGSelectList = [],
            $scope.FySelectList = []
            $scope.submitted = false;
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };
            $scope.dataList = [];
            $scope.ncmStatus = {
                N: '否',
                S: '是'
            };
            $scope.reasonList = [];
            $scope.costPriceShow = false;
            $scope.tbConf = {
                fixedWidth: 100,
                scrollWidth: 1680,
                tableCol: [{
                        type: "",
                        name: "采购计划单号",
                        dataField: "planOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        // clickFn: "viewDetail",
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "中文描述",
                        dataField: "materialsDes",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "机型",
                        dataField: "machineType",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "供货商",
                        dataField: "supplierName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建时间",
                        dataField: "createDateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购方式",
                        dataField: "typeDesc",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "发运方式",
                        dataField: "sendTypeName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "葡语短描述",
                        dataField: "portugueseShort",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "葡语长描述",
                        dataField: "portugueseLong",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        // clickFn: "viewSapError",
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "翻译完成时间",
                        dataField: "transCompTimeShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "NCM",
                        dataField: "ncmCode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "是否完成",
                        dataField: "ncmStatusShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "完成时间",
                        dataField: "ncmTimeShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "材质",
                        dataField: "materialType",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "技术规格/特性",
                        dataField: "specification",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "功能",
                        dataField: "ncmFunction",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "使用位置",
                        dataField: "usePosition",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortField: "",
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "总成/配件",
                        dataField: "accessories",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortField: "",
                        sortType: ""
                    },  
                    {
                        type: "",
                        name: "供货商完成时间",
                        dataField: "supplierTimeShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortField: "",
                        sortType: ""
                    },                                         
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 60,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.editTbConf = {
                scrollWidth: 1280,
                tableCol: [{
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件中文名",
                        width: 200,
                        dataField: "materialsDes",
                        input: false,
                        inputType: ""
                    },
                    // {
                    //     name: "分类",
                    //     width: 150,
                    //     dataField: "materialsType",
                    //     input: false,
                    //     inputType: ""
                    // },
                    {
                        name: "单位",
                        width: 120,
                        dataField: "unit",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "默认储位",
                        width: 180,
                        dataField: "stockLocation",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "实际库存",
                        width: 120,
                        dataField: "enableNum",
                        clickFn: "",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "出库数量",
                        width: 150,
                        dataField: "qty",
                        input: true,
                        inputType: "txt"
                    },
                    {
                        name: "实际出库数量",
                        width: 110,
                        dataField: "realQty",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "出库状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "意向占用",
                        dataField: "intentionNum",
                        width: 100,
                        input: false,
                        clickFn: "intentionDetail",
                        inputType: ""
                    },
                    {
                        name: "发货占用",
                        dataField: "deliveryNum",
                        width: 100,
                        input: false,
                        clickFn: "deliveryDetail",
                        inputType: ""
                    },
                    {
                        name: "单价",
                        width: 150,
                        dataField: "price",
                        input: true,
                        inputType: "price"
                    },
                    {
                        name: "总价",
                        width: 150,
                        dataField: "totalPrice",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "出库金额",
                        width: 150,
                        dataField: "outPrice",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "成本单价",
                        width: 150,
                        dataField: "costPrice",
                        input: false,
                        type: "cost",
                        inputType: ""
                    },
                    {
                        name: "成本小计",
                        width: 150,
                        dataField: "costSubtotal",
                        input: false,
                        type: "cost",
                        inputType: ""
                    },
                ]
            };

            $scope.orderObj = {
                headerInfo: {},
                materialList: []
            };
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/ncm/exportNcmReport"),
                sectionParam: {
                    ids: []
                },
                allUrl: ybHttp.RequestUrl("device", "/ncm/exportNcmReport")
            };
            $scope.importMaterial = function () {
                var param = {
                    name: "NCM",
                    templateReqService: "device",
                    templateReqUrl: "/ncm/download",
                    importReqService: "device",
                    importReqUrl: "//ncm/importNcmReport",
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };

            $scope.outTypeList = [];
            $scope.innerOrderList = [];
            $scope.innerOrder = {
                item: null
            };
            $scope.tabFlag = {};

            var btnPermissionCode = {
                unusualOutAdd: window.ActionCode.unusualOutAdd,
                unusualOutEdit: window.ActionCode.unusualOutEdit,
                unusualOutDel: window.ActionCode.unusualOutDel,
                unusualOutPrint: window.ActionCode.unusualOutPrint,
                unusualOutOut: window.ActionCode.unusualOutOut,
                unusualOutExport: window.ActionCode.unusualOutExport,
                unusualOutObligateAdd: window.ActionCode.unusualOutObligateAdd,
                unusualOutObligatePosting: window.ActionCode.unusualOutObligatePosting,
                mandatoryFinish: window.ActionCode.unusualOutMandatoryFinish,
                showCostPrice: window.ActionCode.showCostPrice,
                unusualOutTJSC: window.ActionCode.unusualOutTJSC,
                unusualOutAudit: window.ActionCode.unusualOutAudit,
            };

            $scope.btnPermission = {
                unusualOutAdd: true,
                unusualOutEdit: true,
                unusualOutDel: true,
                unusualOutPrint: true,
                unusualOutOut: true,
                unusualOutExport: true,
                unusualOutObligateAdd: true,
                unusualOutObligatePosting: true,
                mandatoryFinish: true,
                showCostPrice: true,
                unusualOutTJSC: true,
                unusualOutAudit: true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    fixedWidth: {
                        left: $scope.tbConf.fixedWidth + "px"
                    }
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:

                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.orderObj = {
                    headerInfo: {},
                    materialList: []
                };
                $scope.tabFlag = {};
                $scope.markInfo.allCheckedMaterial = false;
                $scope.toolbarConfig.showMateriel = false;
                $scope.innerOrderList = [];
                $scope.innerOrder = {
                    item: null
                };
            }

            function getDataList(curPage) {
                $scope.markInfo = {
                    allCheckedBill: false,
                    allCheckedMaterial: false,
                    curRow: -1
                };
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/ncm/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.createDateShow = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                        item.ncmTimeShow = $filter("YbDate")(item.ncmTime, "yyyy-MM-dd HH:mm:ss");
                        item.supplierTimeShow = $filter("YbDate")(item.supplierTime, "yyyy-MM-dd HH:mm:ss");
                        item.transCompTimeShow = $filter("YbDate")(item.transCompTime, "yyyy-MM-dd HH:mm:ss");
                        item.ncmStatusShow = $scope.ncmStatus[item.ncmStatus];
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    return resp.value.list
                });
            }

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    case "viewSapError":
                        break;
                    case "alterReport":
                        break;
                    case "intentionDetail":
                        intentionDetail(item);
                        break;
                    case "deliveryDetail":
                        deliveryDetail(item);
                        break;
                    default:
                        break;
                }
            };

            function intentionDetail(item) {
                var param = {
                    materialsNo: item.materialsNo
                };
                ybWidget.IntentionDetail(param);
            }

            function deliveryDetail(item) {
                var param = {
                    materialsNo: item.materialsNo,
                    warehouseCode: $scope.orderObj.headerInfo.outLocation,
                    billNo: $scope.orderObj.headerInfo.abnormalNo
                };
                ybWidget.DeliveryDetail(param);
            }

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                getBillIds();
            };

            $scope.editData = function (item) {
                $scope.tabFlag.type = 1;
                $scope.changeTab(2);
                $scope.detailInfo = item
            };

            $scope.saveData = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var reqUrl = "/ncm/update";
                var param = $scope.detailInfo;                
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList(1);
                    $scope.changeTab(0);
                });
            };
            $scope.getCGSelectList = function() {
                var service = "device";
                var reqUrl = "/purchaseDictionary/list";
                var param = {};
                param.pId = "0";
                 ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    $scope.CGSelectList = resp.value
                    console.log(resp)
                });
            },
            $scope.getFYSelectList = function() {
                var service = "device";
                var reqUrl = "/sendMethodDictionary/list";
                var param = {};
                param.pId = "0";
                 ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    $scope.FYSelectList = resp.value
                });
            },
            $scope.cancel = function () {
                $scope.changeTab(0);
            };
            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
                $scope.getCGSelectList()
                $scope.getFYSelectList()
            })();
        }]);
})(window, window.angular);
